package com.project.service.cryptolib;


import com.project.utils.Util;

import java.util.Arrays;

import static com.project.service.cryptolib.SM4.ECB;


public class Main {

    public static void main(String[] args) {
        byte[] key = {0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef,
                (byte) 0xfe, (byte) 0xdc, (byte) 0xba, (byte) 0x98, 0x76, 0x54, 0x32, 0x10};
        byte[] iv = {0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef,
                (byte) 0xfe, (byte) 0xdc, (byte) 0xba, (byte) 0x98, 0x76, 0x54, 0x32, 0x10};

        byte[] ptext = {0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef,
                (byte) 0xfe, (byte) 0xdc, (byte) 0xba, (byte) 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef,
                (byte) 0xfe, (byte) 0xdc, (byte) 0xba, (byte) 0x98, 0x76, 0x54, 0x32, 0x10};
        byte[] temp = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};


        String str = "你好!abc";
        try {

            System.out.println("--- 原文 ----");
            System.out.println("str: " + str);
            String hexStr = Util.str2HexStr(str);
            System.out.println("hexStr： " + hexStr);

            System.out.println("--- 加密后 ----");
//            SM4 sm4 = new SM4();
//            sm4.Init(ECB, key, iv);
//            sm4.Encrypt(ptext);
            byte[] ctext = CryptoApp.SM4Enc(str, key, iv);

//            System.out.println(Arrays.toString(ctext));
//            System.out.println(Arrays.toString(ptext));
            System.out.println(ctext.length);
            String hexCtext = Util.bytesToHexString(ctext);
            System.out.println("hex密文： " + hexCtext);
            System.out.println("密文长度： " + hexCtext.length());

            System.out.println("--- 解密后 ----");
//            sm4.Init(ECB, key, iv);
////            sm4.Reset(ECB, iv);
//            sm4.Decrypt(ptext);
//            System.out.println(Arrays.toString(ptext));
//            byte[] p = CryptoApp.SM4DecBatch(ctext, key, iv);
//            System.out.println(Arrays.toString(p));

            String pText = CryptoApp.SM4Dec(Util.hexStringToBytes(hexCtext), key, iv);

//            System.out.println(Util.hexStr2Str(pText));
            System.out.println(pText);
//            System.out.println(Arrays.toString(CryptoApp.SM4Dec(ptext, key, iv)));

        } catch (Exception e) {
            e.printStackTrace();
        }
//        try
//        {
//
//            System.out.println("--- 原文 ----");
//            System.out.println("str: " + str);
//
//            String hexStr = Util.str2HexStr(str);
//
//            System.out.println("hexStr： " + hexStr);
//
//            System.out.println(Util.hexStr2Str(hexStr));
//
//            System.out.println("--- 加密后 ----");
////            SM4 sm4 = new SM4();
////            sm4.Init(ECB, key, iv);
////            sm4.Encrypt(ptext);
//            byte[] ctext = CryptoApp.SM4Enc(hexStr, key, iv);
//
//            System.out.println(Arrays.toString(ctext));
////            System.out.println(Arrays.toString(ptext));
//          String hexCtext = Util.bytesToHexString(ctext);
//            System.out.println("hex密文： " + hexCtext);
//            System.out.println("密文长度： " + hexCtext.length());
//            System.out.println("--- 解密后 ----");
////            sm4.Init(ECB, key, iv);
//////            sm4.Reset(ECB, iv);
////            sm4.Decrypt(ptext);
////            System.out.println(Arrays.toString(ptext));
////            byte[] p = CryptoApp.SM4DecBatch(ctext, key, iv);
////            System.out.println(Arrays.toString(p));
//            String pText = CryptoApp.SM4Dec(Util.hexStringToBytes(hexCtext), key, iv);
//
//            System.out.println(Util.hexStr2Str(pText));
////            System.out.println(Arrays.toString(CryptoApp.SM4Dec(ptext, key, iv)));
//
//        }
//        catch (Exception e)
//        {
//            e.printStackTrace();
//        }
    }
}
